package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.AlarmMsg;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.service.business.AlarmMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 实时告警消息
 *
 * @author Leon
 * 2017年2月24日 下午5:51:05
 */
@Validated
@RestController
@RequestMapping(value = "/alarmMsg", produces = BaseController.MEDIA_JSON_UTF8)
public class AlarmMsgController extends BaseController {
    @Autowired
    private AlarmMsgService alarmMsgService;
    @Autowired
    private AccountService accountService;

    /* 分页查询 */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(String userCode, Integer alarmType,
                                @RequestParam(defaultValue = "10") Integer rowCount,
                                @RequestParam(defaultValue = "1") Integer pageNO,
                                HttpServletRequest request) {
        // 获取用户信息
        LoginInfo loginInfo = getLogin(request);
        /*获取总数*/
        Integer total = alarmMsgService.queryCount(loginInfo, userCode, alarmType);
        /*当前返回行数*/
        List<AlarmMsg> alarmMsgs = alarmMsgService.queryPage(loginInfo, userCode, alarmType, rowCount, pageNO);

        return JsonResult.ok(alarmMsgs, total);
    }

    /* 删除 */
    @RequestMapping("/handle")
    public JsonResult handle(@NotNull(message = "ver.msgIds.notNull") String msgIds,
                             HttpServletRequest request) {

        // 获取用户信息
        LoginInfo loginInfo = getLogin(request);
        if ("-1".equals(msgIds)) {
            if (loginInfo.type.equals(UserConstant.USER_AGENT)) {
                alarmMsgService.delAll(loginInfo.id);
            }
            if (loginInfo.type.equals(UserConstant.USER_FRANCHISEE)) {
                List<Account> account = accountService.getOperationAccountIds(loginInfo.id, loginInfo);
                List<Integer> accountIds = new ArrayList<>();
                if (account.size() > 0) {
                    for (Account a : account) {
                        accountIds.add(a.getAccountId());
                    }
                }
                alarmMsgService.delList(accountIds);
            }
        } else {
            List<String> msgIdArray = Arrays.asList(msgIds.split(","));
            alarmMsgService.delBatch(msgIdArray);
        }

        return JsonResult.ok();
    }
}